package com.steel.yfmall.cloud.assistant.zg.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.steel.yfmall.cloud.assistant.dto.para.view.ParaOrderSettlementBean;
import com.steel.yfmall.cloud.assistant.zg.ZgSOService;
import com.steel.yfmall.cloud.assistant.dto.para.account.ParaAccountSyncAccount;
import com.steel.yfmall.cloud.assistant.dto.zg.ZgCallWsdlAPIBaseDTO;
import com.steel.yfmall.cloud.assistant.dto.zg.ZgSOResultBean;
import com.steel.yfmall.cloud.assistant.dto.zg.ZgViewBean;
import com.steel.yfmall.cloud.assistant.dto.zg.ZgpageData;
import com.steel.yfmall.cloud.assistant.utils.zg.ZgCallServiceUtils;
import com.steel.yfmall.cloud.assistant.utils.zg.ZgMethodBeanUtil;
import com.steel.yfmall.cloud.assistant.utils.zg.ZgParamUtils;
import com.steel.yfmall.cloud.components.utils.base.APIMsgBean;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * 请求中冠系统
 * 请求接口集获取视图数据
 * 工具包
 *
 * @author jia chang bao
 * @date 2020/11/5 10:51
 */
@Service
@Slf4j
public class ZgSOServiceImpl implements ZgSOService {
    private static final Logger logger = Logger.getLogger(ZgSOService.class);

    @Value("${jyjt_zgSystem_soWSKEC_url}")
    private String ZG_SO_URL;

    @Resource
    private ZgCallServiceUtils zgCallServiceUtils;

    @Override
    public ZgSOResultBean getViewData(ZgViewBean zgViewBean) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("viewId", zgViewBean.getViewId());
        paramMap.put("pageNum", zgViewBean.getPageNum());
        paramMap.put("param", zgViewBean.getParam());
        paramMap.put("pageSize",zgViewBean.getPageSize());
        //填充方法信息
        ZgCallWsdlAPIBaseDTO wsdlUtilBean = new ZgCallWsdlAPIBaseDTO();
        //请求地址
        wsdlUtilBean.setUrl(ZG_SO_URL);
        //方法名
        wsdlUtilBean.setInterfaceName(zgViewBean.getMethod());
        //参数名称
        wsdlUtilBean.setParameterType(ZgMethodBeanUtil.SO_SYSTEM);
        //请求中冠
        APIMsgBean<String> serviceDataBean = zgCallServiceUtils.commonCallZgSoApi(ZgParamUtils.transToUpperObject(JSON.toJSONString(paramMap)), wsdlUtilBean);
        //解析返回数据
        ZgSOResultBean zgSOResultBean = new ZgSOResultBean();
        if (serviceDataBean.getData() != null) {
            //开始解析中冠数据
            try {
                JSONObject jsonObject = JSONObject.parseObject(serviceDataBean.getData().toString());
                zgSOResultBean.setIsSuccess(jsonObject.get("ISSUCCESS").toString());
                zgSOResultBean.setMsg(jsonObject.get("MSG").toString());
                zgSOResultBean.setData(jsonObject.get("DATA").toString());
                //解析转换分页信息
                ZgpageData pagedata = JSONObject.parseObject(jsonObject.get("PAGEDATA").toString(), ZgpageData.class);
                zgSOResultBean.setZgpageData(pagedata);
                return zgSOResultBean;
            } catch (Exception e) {
                zgSOResultBean.setIsSuccess("N");
                zgSOResultBean.setMsg("解析转换中冠信息异常错误信息：" + e);
            }
        } else {
            zgSOResultBean.setMsg(serviceDataBean.getSoaStateBean().getMessage());
            zgSOResultBean.setIsSuccess("N");
        }
        return zgSOResultBean;
    }

    /***
     * @Title: 查询余额信息
     * @Description: (这里用一句话描述这个方法的作用)
     * @date 2020/12/10 14:11
     * @author lang
     * @param [paraBean, zgCallWsdlAPIBaseDTO]
     * @return com.steel.yfmall.cloud.assistant.dto.zg.ZgSOResultBean    返回类型
     * @version 1.0
     */
    @Override
    public ZgSOResultBean getAccountDate(ParaAccountSyncAccount paraBean, ZgCallWsdlAPIBaseDTO zgCallWsdlAPIBaseDTO) {
        if (paraBean == null || StringUtils.isBlank(paraBean.getCustNo())) {
            logger.error("必传参数为空,无法进行查询!");
            return null;
        }
        //填充方法信息
        ZgCallWsdlAPIBaseDTO wsdlUtilBean = new ZgCallWsdlAPIBaseDTO();
        //请求地址
        wsdlUtilBean.setUrl(zgCallWsdlAPIBaseDTO.getUrl());
        //方法名
        wsdlUtilBean.setInterfaceName(zgCallWsdlAPIBaseDTO.getInterfaceName());
        //参数名称
        wsdlUtilBean.setParameterType(zgCallWsdlAPIBaseDTO.getParameterType());
        //请求中冠
        APIMsgBean<String> serviceDataBean = zgCallServiceUtils.commonCallZgSoApi(ZgParamUtils.transToUpperObject(JSON.toJSONString(paraBean)), wsdlUtilBean);
        //解析返回数据
        ZgSOResultBean zgSOResultBean = new ZgSOResultBean();
        if (serviceDataBean.getData() != null) {
            //开始解析中冠数据
            try {
                JSONObject jsonObject = JSONObject.parseObject(serviceDataBean.getData().toString());
                zgSOResultBean.setIsSuccess(jsonObject.get("ISSUCCESS").toString());
                zgSOResultBean.setMsg(jsonObject.get("MSG").toString());
                zgSOResultBean.setData(jsonObject.get("DATA").toString());
                return zgSOResultBean;
            } catch (Exception e) {
                zgSOResultBean.setIsSuccess("N");
                zgSOResultBean.setMsg("解析转换中冠信息异常错误信息：" + e);
            }
        } else {
            zgSOResultBean.setMsg(serviceDataBean.getSoaStateBean().getMessage());
            zgSOResultBean.setIsSuccess("N");
        }
        return zgSOResultBean;
    }

    /**
     * 根据 订单号 和 客户编号 查询结算单信息
     *
     * @param paraOrderSettlementBean
     * @return 结果
     * @author jia chang bao
     */
    @Override
    public ZgSOResultBean getOrderSettlement(ParaOrderSettlementBean paraOrderSettlementBean, ZgCallWsdlAPIBaseDTO zgCallWsdlAPIBaseDTO) {
        //填充方法信息
        ZgCallWsdlAPIBaseDTO wsdlUtilBean = new ZgCallWsdlAPIBaseDTO();
        //请求地址
        wsdlUtilBean.setUrl(zgCallWsdlAPIBaseDTO.getUrl());
        //方法名
        wsdlUtilBean.setInterfaceName(zgCallWsdlAPIBaseDTO.getInterfaceName());
        //参数名称
        wsdlUtilBean.setParameterType(zgCallWsdlAPIBaseDTO.getParameterType());
        //请求中冠
        APIMsgBean<String> serviceDataBean = zgCallServiceUtils.commonCallZgSoApi(ZgParamUtils.transToUpperObject(JSON.toJSONString(paraOrderSettlementBean)), wsdlUtilBean);
        //解析返回数据
        ZgSOResultBean zgSOResultBean = new ZgSOResultBean();
        if (serviceDataBean.getData() != null) {
            //开始解析中冠数据
            try {
                JSONObject jsonObject = JSONObject.parseObject(serviceDataBean.getData().toString());
                zgSOResultBean.setIsSuccess(jsonObject.get("ISSUCCESS").toString());
                zgSOResultBean.setMsg(jsonObject.get("MSG").toString());
                zgSOResultBean.setData(jsonObject.get("DATA").toString());
                return zgSOResultBean;
            } catch (Exception e) {
                zgSOResultBean.setIsSuccess("N");
                zgSOResultBean.setMsg("解析转换中冠信息异常错误信息：" + e);
            }
        } else {
            zgSOResultBean.setMsg(serviceDataBean.getSoaStateBean().getMessage());
            zgSOResultBean.setIsSuccess("N");
        }
        return zgSOResultBean;
    }
}
